package org.duiduo.common.runtime.redis;

import cn.hutool.extra.spring.EnableSpringUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * redisson分布式锁配置
 *
 * @author liangze
 * @date 2023/8/10 14:17
 */
@Slf4j
@Order(1)
@Configuration
@EnableSpringUtil
@ConditionalOnProperty(name = "redisson.enable", havingValue = "true")
public class RedissonConfig {

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        log.info("::开始加载Redission配置");
        String host = SpringUtil.getProperty("spring.data.redis.host");
        String port = SpringUtil.getProperty("spring.data.redis.port");
        String password = SpringUtil.getProperty("spring.data.redis.password");
        Integer database = SpringUtil.getProperty("spring.data.redis.database", Integer.class, 0);

        Config config = new Config();
        config.useSingleServer()
                .setAddress(String.format("redis://%s:%s", host, port))
                .setPassword(password)
                .setDatabase(database);
        return Redisson.create(config);
    }

}
